import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from "./routes";
import {isLogin} from "../utils/user";

Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

 var routeStack = ["/"];


router.beforeEach((to,from,next)=>{
  if(to.meta.isAuth){
    if(isLogin()){
      next();
    }else{
      next()
    }
  }else{
    next()
  }
})


function repaceRoute(path){
  routeStack.pop();
  routeStack.push(path);
  router.replace(path)
}



function pushRouteByQuery(path,params){
  routeStack.push(path);
  router.push({
    path:path,
    query:params
  })
}

function pushRouteByName(name,params){
  routeStack.push(name);
  router.push({
    name:name,
    params:params
  })
}

function backRoute(){
  routeStack.pop();
  router.back();
}

//router添加对应方法
router.repaceRoute = repaceRoute;
router.pushRouteByQuery = pushRouteByQuery;
router.pushRouteByName = pushRouteByName;
router.backRoute = backRoute;
router.routeStack = routeStack;

export default router
